package com.lzx.indicatordemo.demo;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.framework.view.indicator.BaseIndicatorAdapter;
import com.framework.view.indicator.GodIndicator;
import com.framework.view.indicator.GodIndicatorAdapter;
import com.framework.view.indicator.LayoutIndicatorAdapter;
import com.framework.view.indicator.configs.GlobalConfig;
import com.framework.view.indicator.configs.LayoutConfig;
import com.framework.view.indicator.configs.MidDividerConfig;
import com.framework.view.indicator.configs.TextTitleConfig;
import com.framework.view.indicator.indicators.configs.LineIndicatorConfig;
import com.framework.view.indicator.indicators.configs.WrapIndicatorConfig;
import com.framework.view.indicator.listener.OnItemClickListener;
import com.framework.view.indicator.listener.OnSimpleScrollResultListener;
import com.framework.view.indicator.model.IndicatorWidthModel;
import com.lzx.indicatordemo.Constants;
import com.lzx.indicatordemo.ExamplePagerAdapter;
import com.lzx.indicatordemo.R;

import java.util.Arrays;
import java.util.List;

import static com.lzx.indicatordemo.Constants.CHANNELS;
import static com.lzx.indicatordemo.Constants.CHANNELS2;
import static com.lzx.indicatordemo.Constants.normalTxtColors;

/**
 * description
 *
 * @author liangzx
 * @version 1.0
 * @time 2020-04-21 18:03
 **/
public class CustomLayoutTitleActivity extends AppCompatActivity {

    private List<String> mDataList = Arrays.asList(CHANNELS);
    private ViewPager mViewPager;
    private ExamplePagerAdapter mExamplePagerAdapter = new ExamplePagerAdapter(mDataList);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_layout_title_demo_activity);

        mViewPager = findViewById(R.id.vp);
        mViewPager.setAdapter(mExamplePagerAdapter);
        init1();
        init2();
        init3();
        init4();
    }

    private void init1() {
        GodIndicator godIndicator = findViewById(R.id.g1);
        LayoutIndicatorAdapter adapter;
        godIndicator.setBackgroundColor(Constants.randomBgColor());
        godIndicator.setAdapter(adapter = new LayoutIndicatorAdapter<WrapIndicatorConfig, String>(R.layout.custom_layout_item_one,
                Arrays.asList(CHANNELS2)) {

            @Override
            protected void bindGlobalConfig(GlobalConfig config) {
                config.setCanOffScreen(false);
            }

            @Override
            protected LayoutConfig bindTitleConfig() {
                return LayoutConfig.factory()
                        .setNeedAutoChangeTxtIds(R.id.name_tv, R.id.desc_tv)
                        .setNormalColor(Color.BLACK)
                        .setSelectedColors(Color.BLUE, Color.YELLOW)
                        .setColorGradientFollowSlide(true)
                        .setSizeFollowSlideChange(true)
                        .setPendingLeft(20)
                        .setPendingRight(20)
                        .setPendingBottom(10)
                        .setPendingTop(10)
                        .setNormalSize(30)
                        .setSelectedSize(40)
                        .setSelectedBold(true);
            }

            @Override
            protected View bindItemTitleView(View view, int position, String data) {
                TextView nameTv = view.findViewById(R.id.name_tv);
                TextView descTv = view.findViewById(R.id.desc_tv);
                nameTv.setText(data);
                descTv.setText(data + position);
                return view;
            }

            @Override
            protected WrapIndicatorConfig bindIndicatorConfig() {
                return WrapIndicatorConfig.factory()
                        .setColor(Color.parseColor("#96FFFFFF"));
            }

        });
        adapter.setOnScrollResultListener(new OnSimpleScrollResultListener() {
            @Override
            public void onSelected(BaseIndicatorAdapter adapter, int index, int totalCount) {
                Log.e(CustomLayoutTitleActivity.class.getName(), "g1:onSelected:index" + index + " totalCount:" + totalCount);
            }

            @Override
            public void onCancelSelected(BaseIndicatorAdapter adapter, int index, int totalCount) {
                Log.e(CustomLayoutTitleActivity.class.getName(), "g1:onCancelSelected:index" + index + " totalCount:" + totalCount);
            }

            @Override
            public void onEntering(BaseIndicatorAdapter adapter, int index, int totalCount, float enterPercent, boolean leftToRight) {
                Log.e(CustomLayoutTitleActivity.class.getName(), "g1:onEntering:index" + index + " totalCount:" + totalCount + " enterPercent:" + enterPercent);
            }

            @Override
            public void onLeaving(BaseIndicatorAdapter adapter, int index, int totalCount, float leavePercent, boolean leftToRight) {
                Log.e(CustomLayoutTitleActivity.class.getName(), "g1:onLeaving:index" + index + " totalCount:" + totalCount + " leavePercent:" + leavePercent);
            }

            @Override
            public void onTitleClick(int position) {
                Log.e(CustomLayoutTitleActivity.class.getName(), "position" + position);
            }
        });
        adapter.bindViewPager(mViewPager);
    }

    private void init2() {
        GodIndicator godIndicator = findViewById(R.id.g2);
        LayoutIndicatorAdapter adapter;
        godIndicator.setBackgroundColor(Constants.randomBgColor());
        godIndicator.setAdapter(adapter = new LayoutIndicatorAdapter<LineIndicatorConfig, String>(R.layout.custom_layout_item_one,
                Arrays.asList(CHANNELS2)) {

            @Override
            protected void bindGlobalConfig(GlobalConfig config) {
                config.setCanOffScreen(false);
            }

            @Override
            protected LayoutConfig bindTitleConfig() {
                return LayoutConfig.factory()
                        .setNeedAutoChangeTxtIds(R.id.name_tv, R.id.desc_tv)
                        .setNormalColor(Color.BLACK)
                        .setSelectedColors(Color.BLUE, Color.YELLOW)
                        .setPendingLeft(20)
                        .setPendingRight(20)
                        .setPendingBottom(10)
                        .setPendingTop(10)
                        .setNormalSize(30)
                        .setSelectedSize(40)
                        .setNormalBold(true)
                        .setSelectedBold(true);
            }

            @Override
            protected View bindItemTitleView(View view, int position, String data) {
                TextView nameTv = view.findViewById(R.id.name_tv);
                TextView descTv = view.findViewById(R.id.desc_tv);
                nameTv.setText(data);
                descTv.setText(data + position);
                return view;
            }

            @Override
            protected LineIndicatorConfig bindIndicatorConfig() {
                return LineIndicatorConfig.factory()
                        .setColor(Constants.randomWrapIndicatorColor())
                        .setWidthModel(IndicatorWidthModel.WARP_CONTENT)
                        .setPendingLeft(10)
                        .setPendingRight(10);
        }

        });
        adapter.bindViewPager(mViewPager);
    }

    private void init3() {
        GodIndicator godIndicator = findViewById(R.id.g3);
        LayoutIndicatorAdapter adapter;
        godIndicator.setBackgroundColor(Constants.randomBgColor());
        godIndicator.setAdapter(adapter = new LayoutIndicatorAdapter<LineIndicatorConfig, String>(R.layout.custom_layout_item_two,
                Arrays.asList(CHANNELS)) {
            @Override
            protected LayoutConfig bindTitleConfig() {
                return LayoutConfig.factory()
                        .setNeedAutoChangeTxtIds(R.id.name_tv, R.id.desc_tv)
                        .setNormalColor(Color.BLACK)
                        .setSelectedColors(Color.BLUE, Color.YELLOW)
                        .setPendingLeft(20)
                        .setPendingRight(20)
                        .setNormalSize(30)
                        .setSelectedSize(40)
                        .setSelectedBold(true)
                        .bindClickIds(R.id.name_tv, R.id.desc_tv, R.id.icon_iv);
            }

            @Override
            protected View bindItemTitleView(View view, int position, String data) {
                TextView nameTv = view.findViewById(R.id.name_tv);
                TextView descTv = view.findViewById(R.id.desc_tv);
                ImageView iconIv = view.findViewById(R.id.icon_iv);
                nameTv.setText(data);
                descTv.setText(data + position);
                iconIv.setImageResource(R.mipmap.down);
                iconIv.setTag(0);
                return view;
            }

            @Override
            protected LineIndicatorConfig bindIndicatorConfig() {
                return LineIndicatorConfig.factory()
                        .setColor(Constants.randomWrapIndicatorColor())
                        .setPendingLeft(10)
                        .setPendingRight(10);
            }

        });
        adapter.bindViewPager(mViewPager);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(LayoutIndicatorAdapter adapter, View parentView, View view, int position) {
                String tips = "点击了";
                if (view.getId() == R.id.name_tv) {
                    tips += "名称";
                } else if (view.getId() == R.id.desc_tv) {
                    tips += "描述";
                } else if (view.getId() == R.id.icon_iv) {
                    tips += "图标";
                }
                ImageView imageView = parentView.findViewById(R.id.icon_iv);
                if (0 == ((int) imageView.getTag())) {
                    imageView.setImageResource(R.mipmap.up);
                    imageView.setTag(1);
                } else {
                    imageView.setImageResource(R.mipmap.down);
                    imageView.setTag(0);
                }
                tips += " 位置:" + position;
                Toast.makeText(CustomLayoutTitleActivity.this, tips, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void init4() {
        GodIndicator godIndicator = findViewById(R.id.g4);
        final LayoutIndicatorAdapter adapter;
        godIndicator.setBackgroundColor(Constants.randomBgColor());
        godIndicator.setAdapter(adapter = new LayoutIndicatorAdapter<LineIndicatorConfig, String>(R.layout.custom_layout_item_two,
                Arrays.asList(CHANNELS)) {
            @Override
            protected LayoutConfig bindTitleConfig() {
                return LayoutConfig.factory()
                        .setNeedAutoChangeTxtIds(R.id.name_tv, R.id.desc_tv)
                        .setNormalColor(Color.BLACK)
                        .setSelectedColors(Color.BLUE, Color.YELLOW)
                        .setPendingLeft(20)
                        .setPendingRight(20)
                        .setNormalSize(30)
                        .setSelectedSize(40)
                        .setMidDividerConfig(MidDividerConfig.factory()
                                .setColor(Color.WHITE)
                                .setWidth(10)
                                .setHeight(LinearLayout.LayoutParams.MATCH_PARENT)
                                .setRoundRadius(10)
                                .setMarginLeft(20)
                                .setMarginRight(20)
                                .setMarginBottom(20)
                                .setMarginTop(20));
            }

            @Override
            protected View bindItemTitleView(View view, int position, String data) {
                TextView nameTv = view.findViewById(R.id.name_tv);
                TextView descTv = view.findViewById(R.id.desc_tv);
                ImageView iconIv = view.findViewById(R.id.icon_iv);
                nameTv.setText(data);
                descTv.setText(data + position);
                iconIv.setImageResource(R.mipmap.down);
                iconIv.setTag(0);
                return view;
            }

            @Override
            protected LineIndicatorConfig bindIndicatorConfig() {
                return LineIndicatorConfig.factory()
                        .setColor(Constants.randomWrapIndicatorColor())
                        .setPendingLeft(10)
                        .setPendingRight(10);
            }

        });
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(LayoutIndicatorAdapter adapter, View parentView, View view, int position) {
                String tips = "点击了";
                if (view.getId() == R.id.name_tv) {
                    tips += "名称";
                } else if (view.getId() == R.id.desc_tv) {
                    tips += "描述";
                } else if (view.getId() == R.id.icon_iv) {
                    tips += "图标";
                } else {
                    tips += "父控件";
                }
                ImageView imageView = parentView.findViewById(R.id.icon_iv);
                if (0 == ((int) imageView.getTag())) {
                    imageView.setImageResource(R.mipmap.up);
                    imageView.setTag(1);
                } else {
                    imageView.setImageResource(R.mipmap.down);
                    imageView.setTag(0);
                }
                tips += " 位置:" + position;
                Toast.makeText(CustomLayoutTitleActivity.this, tips, Toast.LENGTH_SHORT).show();
            }
        });
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                adapter.bindViewPager(mViewPager);
            }
        }, 3000);
    }

    private void init5() {
        GodIndicator godIndicator = findViewById(R.id.g5);
        godIndicator.setBackgroundColor(Constants.randomBgColor());
        godIndicator.setAdapter(new GodIndicatorAdapter<WrapIndicatorConfig>(mViewPager) {
            @Override
            protected TextTitleConfig bindTitleConfig() {
                return TextTitleConfig.factory()
                        .setNormalColors(normalTxtColors)
                        .setSelectedColor(Constants.randomIndicatorColor())
                        .setUseColorClip(true)
                        .setSizeFollowSlideChange(true)
                        .setPendingLeft(20)
                        .setPendingRight(20)
                        .setNormalSize(30)
                        .setSelectedSize(40);
            }

            @Override
            public String getTitleName(int position) {
                return CHANNELS[position];
            }

            @Override
            public int getCount() {
                return CHANNELS.length;
            }

            @Override
            protected WrapIndicatorConfig bindIndicatorConfig() {
                return WrapIndicatorConfig.factory()
                        .setColors(Constants.wrapColors)
                        .setColorFollowSlideChange(true)
                        .setPendingLeft(10)
                        .setPendingTop(10)
                        .setPendingRight(10)
                        .setPendingBottom(10);
            }

        });
    }

    private void init6() {
        GodIndicator godIndicator = findViewById(R.id.g6);
        godIndicator.setBackgroundColor(Constants.randomBgColor());
        godIndicator.setAdapter(new GodIndicatorAdapter<WrapIndicatorConfig>(mViewPager) {
            @Override
            protected TextTitleConfig bindTitleConfig() {
                return TextTitleConfig.factory()
                        .setNormalColors(normalTxtColors)
                        .setSelectedColor(Constants.randomIndicatorColor())
                        .setUseColorClip(true)
                        .setSizeFollowSlideChange(true)
                        .setPendingLeft(20)
                        .setPendingRight(20)
                        .setNormalSize(30)
                        .setSelectedSize(40);
            }

            @Override
            public String getTitleName(int position) {
                return CHANNELS[position];
            }

            @Override
            public int getCount() {
                return CHANNELS.length;
            }

            @Override
            protected WrapIndicatorConfig bindIndicatorConfig() {
                return WrapIndicatorConfig.factory()
                        .setStroke(true, 10)
                        .setColor(Constants.color("#96F0F0F0"))
                        .setColorFollowSlideChange(true)
                        .setPendingLeft(10)
                        .setPendingTop(10)
                        .setPendingRight(10)
                        .setPendingBottom(10);
            }

        });
    }
}
